<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <title>图书管理-登录</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div style="width:20%;margin:0 auto; text-align: center; padding-top:5% " id="app">
    <p id="msg" style="color:red"></p>
    <form action="" id="myform" method="post">
        <div class="form-group form-inline">
            <label for="username">用户名：</label>
            <input type="text" class="form-control" v-model="username" id="username" name="account" placeholder="请输入用户名">
        </div>
        <div class="form-group form-inline">
            <label for="password">密&nbsp;&nbsp;&nbsp;&nbsp;码：</label>
            <input type="password" class="form-control" v-model="password" id="password" name="password" placeholder="请输入密码"/>
        </div>
        <div class="form-group">
            <button class="btn btn-success btn-md" @click="login()" id="btnLogin" type="button">登录</button>
        </div>
    </form>
</div>

<script>
    let vueApp = new Vue({
        el: "#app",
        data() {
            return {
                    username : "",
                    password : ""
            }
        },
        methods: {
            login(){
                let param = new URLSearchParams({
                    "username" : this.username,
                    "password" : this.password
                });
                axios.post("/fzq/login", param.toString())
                    .then(res => {
                        if(res.data.status == 200) {
                            alert("登录成功")
                            console.log(JSON.stringify(res.data.data))
                            //window.localStorage.setItem("token", res.data.data.token);
                            //window.localStorage.setItem('username',res.data.data.username);
                            const token = res.data.data.token;
                            const username = res.data.data.username;
                            Cookies.set('token', token, { secure: true, sameSite: 'strict' });
                            Cookies.set('username', username, { secure: true, sameSite: 'strict' });
                            window.location.href="/fzq/html/index.html"
                        }else{
                            alert(res.data.msg)
                        }
                    })
                    .catch(err => {
                        alert("服务器去火星了")
                    });
            }
        }, created() {

        }
    });
</script>
</body>
</html>